package lib.bone.admin.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lib.bone.access.module.post.NepuPostService;
import lib.bone.access.module.post.domain.NepuPost;
import lib.bone.system.domain.dto.user.UserUpdateImportDTO;
import lib.bone.system.domain.dto.user.UserdeleteDTO;
import lib.bone.system.domain.entity.SysDept;
import lib.bone.system.domain.entity.SysUser;
import lib.bone.system.service.SysDeptService;
import lib.bone.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Slf4j
public class UserDeleteListener extends AnalysisEventListener<UserdeleteDTO> {

    /**
     * 用户服务
     */
    SysUserService userService;
    /**
     * 部门服务
     */
    SysDeptService deptService;

    NepuPostService nepuPostService;

    public UserDeleteListener(){}

    public UserDeleteListener(SysUserService userService, SysDeptService deptService, NepuPostService nepuPostService){
        this.userService = userService;
        this.deptService = deptService;
        this.nepuPostService = nepuPostService;
    }


    @Override
    @Transactional
    public void invoke(UserdeleteDTO userdeleteDTO, AnalysisContext analysisContext) {
            //1.查询用户是否已经存在
            Optional<SysUser> userOp = userService.findByUserName(userdeleteDTO.getUserName());
            if (userOp.isPresent()){
                SysUser sysUser = userOp.get();
                userService.deleteById(sysUser.getId());
                log.info("{}用户已被删除！",userdeleteDTO.getNickName());
            }else {
                log.info("{}用户不存在！",userdeleteDTO.getNickName());

            }
            log.info("{},{}导入成功", userdeleteDTO.getUserName(), userdeleteDTO.getNickName());
        }


//    @Override
//    @Transactional
//    public void invoke(UserdeleteDTO userdeleteDTO, AnalysisContext analysisContext) {
//        //1.查询用户是否已经存在
//        Optional<SysUser> userOp = userService.findByUserName(userdeleteDTO.getUserName());
//        if (userOp.isPresent()){
//
//        }else {
//            log.info("{}用户不存在！",userdeleteDTO.getNickName());
//
//        }
//    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("用户删除完成");
    }
}
